﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Xml.Serialization;
using System.IO;
using System.ComponentModel;
using System.Runtime.Serialization.Formatters.Soap;

namespace DicomScu.Scu
{
    [Serializable]
    public class ScuConfigCollection : IEnumerable
    {
        public ArrayList ConfigCollection
        {
            get;
            set;
        }

        public ScuConfigCollection()
        {
            ConfigCollection = new ArrayList();
        }

        public ScuConfig GetCurrentScu()
        {
            if (ConfigCollection.Count > 0 && ConfigCollection[0] != null)
            {
                return (ScuConfig)ConfigCollection[0];
            }
            else
                return null;
        }

        public void SetCurrentScu(ScuConfig config)
        {
            if (ConfigCollection.Contains(config))
            {
                ConfigCollection.Remove(config);
                ConfigCollection.Insert(0, config);
            }
            else
            {
                throw new Exception("Config is not contained in Collection");
            }
        }

        public virtual void MoveUp(ScuConfig config)
        {
            if (ConfigCollection.Contains(config))
            {
                int index = ConfigCollection.IndexOf(config);
                if (index > 0)
                {
                    ConfigCollection.Remove(config);
                    ConfigCollection.Insert(index - 1, config);
                }
            }
        }
        public void MoveDown(ScuConfig config)
        {
            if (ConfigCollection.Contains(config))
            {
                int index = ConfigCollection.IndexOf(config);
                if (index < ConfigCollection.Count - 1)
                {
                    ConfigCollection.Remove(config);
                    ConfigCollection.Insert(index + 1, config);
                }
            }
        }
        public void Save2File(string ConfigPath)
        {
            SoapFormatter formatter = new SoapFormatter();

            using (FileStream fs = new FileStream(ConfigPath, FileMode.Create))
            {
                try
                {
                    formatter.Serialize(fs, this);
                    fs.Flush();
                    fs.Close();
                }
                catch
                {

                }
            }
        }

        public virtual void MoveFirst(ScuConfig config)
        {
            if (ConfigCollection.Contains(config))
            {
                int index = ConfigCollection.IndexOf(config);
                if (index > 0)
                {
                    ConfigCollection.Remove(config);
                    ConfigCollection.Insert(0, config);
                }
            }
        }
        public void MoveLast(ScuConfig config)
        {
            if (ConfigCollection.Contains(config))
            {
                int index = ConfigCollection.IndexOf(config);
                if (index < ConfigCollection.Count - 1)
                {
                    ConfigCollection.Remove(config);
                    ConfigCollection.Insert(ConfigCollection.Count - 1, config);
                }
            }
        }

        public static ScuConfigCollection GetFromFile(string ConfigPath)
        {
            ScuConfigCollection col;
            if (!File.Exists(ConfigPath))
            {
                col = new ScuConfigCollection();
            }
            else
            {
                SoapFormatter formatter = new SoapFormatter();
                using (FileStream fs = new FileStream(ConfigPath, FileMode.Open))
                {
                    try
                    {
                        col = (ScuConfigCollection)formatter.Deserialize(fs);
                    }
                    catch
                    {
                        col = new ScuConfigCollection();
                    }
                }
            }
            return col;
        }


        #region IEnumerable Members

        public IEnumerator GetEnumerator()
        {
            return ConfigCollection.GetEnumerator();
        }

        #endregion
    }
}
